<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="Mouweng">





<title>反射 | Mouweng&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 5.1.1"><link rel="alternate" href="/atom.xml" title="Mouweng's Blog" type="application/atom+xml">
</head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">Mouweng</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">Mouweng</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">反射</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">Mouweng</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">August 27, 2020&nbsp;&nbsp;23:41:12</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/java%E5%9F%BA%E7%A1%80/">java基础</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h2 id="1-反射的定义"><a href="#1-反射的定义" class="headerlink" title="1.反射的定义"></a>1.反射的定义</h2><p><strong>动态语言</strong></p>
<blockquote>
<p>动态语言是指在运行阶段可以改变其结构</p>
<p>Java、C++、C是静态语言，Java可以通过反射获得类似动态语言的特性。</p>
</blockquote>
<p><strong>反射</strong></p>
<blockquote>
<p>反射机制允许程序在执行期借助于Reflection API取得任何类的内部信息，并能直接操作任意对象的内部属性及方法。</p>
</blockquote>
<p><strong>对比</strong></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gg95cc2g3rj31gq096wmw.jpg" alt="正常方式和反射方式的对比"></p>
<p><strong>反射的优缺点</strong></p>
<ul>
<li>优点：可以动态创建对象和编译，灵活</li>
<li>缺点： 对象性能有影响，总是慢于直接执行相同的操作</li>
</ul>
<h2 id="2-Class类"><a href="#2-Class类" class="headerlink" title="2.Class类"></a>2.Class类</h2><h3 id="2-1-什么是Class类"><a href="#2-1-什么是Class类" class="headerlink" title="2.1 什么是Class类"></a>2.1 什么是Class类</h3><ul>
<li>class本身也是一个类</li>
<li>只能由系统建立对象</li>
<li>一个加载的类在JVM中只会有一个Class实例</li>
<li>一个Class实例对应的是一个加载到JVM中的一个.class文件</li>
<li>每个类的实例都会记得自己是由哪个class实例生成的</li>
<li>通过Class可以完整的得到一个类所有被加载的结构</li>
<li>Class类是Reflection反射的根源，针对任何想动态加载，运行的类，唯有先获得相应的Class对象</li>
</ul>
<h3 id="2-2-获取Class类的方法"><a href="#2-2-获取Class类的方法" class="headerlink" title="2.2 获取Class类的方法"></a>2.2 获取Class类的方法</h3><h4 id="1-通过对象获得"><a href="#1-通过对象获得" class="headerlink" title="1) 通过对象获得"></a>1) 通过对象获得</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Class c1 = person.getClass();</span><br></pre></td></tr></table></figure>

<h4 id="2-forName获得"><a href="#2-forName获得" class="headerlink" title="2) forName获得"></a>2) forName获得</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Class c2 = Class.forName(<span class="string">&quot;com.mouweng.Student&quot;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="3-通过类名-class获得"><a href="#3-通过类名-class获得" class="headerlink" title="3) 通过类名.class获得"></a>3) 通过类名.class获得</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Class c3 = Student.class;</span><br></pre></td></tr></table></figure>

<h4 id="4-包装类Type属性"><a href="#4-包装类Type属性" class="headerlink" title="4) 包装类Type属性"></a>4) 包装类Type属性</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Class c4 = Integer.TYPE;</span><br></pre></td></tr></table></figure>

<h4 id="5-获得父类类型"><a href="#5-获得父类类型" class="headerlink" title="5) 获得父类类型"></a>5) 获得父类类型</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Class c5 = c1.getSuperclass();</span><br></pre></td></tr></table></figure>

<h3 id="2-3-哪些类型有Class"><a href="#2-3-哪些类型有Class" class="headerlink" title="2.3 哪些类型有Class"></a>2.3 哪些类型有Class</h3><blockquote>
<p>类、接口、一维数组、二维数组、注解、枚举、基本数据类型、void、Class</p>
</blockquote>
<p> 只要元素类型一样，就是同一个Class</p>
<h2 id="3-获取类运行时的结构"><a href="#3-获取类运行时的结构" class="headerlink" title="3.获取类运行时的结构"></a>3.获取类运行时的结构</h2><h3 id="3-1-获取类的名字"><a href="#3-1-获取类的名字" class="headerlink" title="3.1 获取类的名字"></a>3.1 获取类的名字</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Class c1 = Class.forName(<span class="string">&quot;com.mouweng.User&quot;</span>);</span><br><span class="line"><span class="comment">// 获得类的名字</span></span><br><span class="line">System.out.println(c1.getName());</span><br><span class="line">System.out.println(c1.getSimpleName());</span><br></pre></td></tr></table></figure>

<h3 id="3-2-类的属性"><a href="#3-2-类的属性" class="headerlink" title="3.2 类的属性"></a>3.2 类的属性</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">Class c1 = Class.forName(<span class="string">&quot;com.mouweng.User&quot;</span>);</span><br><span class="line"><span class="comment">// 获得类的属性</span></span><br><span class="line">Field[] fields = c1.getFields();<span class="comment">// 只能找到public属性</span></span><br><span class="line"><span class="keyword">for</span> (Field field : fields) &#123;</span><br><span class="line">    System.out.println(field);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">System.out.println();</span><br><span class="line">fields = c1.getDeclaredFields();<span class="comment">// 找到全部的属性</span></span><br><span class="line"><span class="keyword">for</span> (Field field : fields) &#123;</span><br><span class="line">    System.out.println(field);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">System.out.println();</span><br><span class="line"><span class="comment">// 获得指定类的属性的值</span></span><br><span class="line">Field field = c1.getDeclaredField(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">System.out.println(field);</span><br></pre></td></tr></table></figure>

<h3 id="3-3-类的方法"><a href="#3-3-类的方法" class="headerlink" title="3.3 类的方法"></a>3.3 类的方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">Class c1 = Class.forName(<span class="string">&quot;com.mouweng.User&quot;</span>);</span><br><span class="line"><span class="comment">// 获得方法</span></span><br><span class="line">Method[] methods = c1.getMethods();</span><br><span class="line"><span class="keyword">for</span> (Method method : methods) &#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;正常的：&quot;</span> + method); <span class="comment">// 获得本类及其父类的全部public方法</span></span><br><span class="line">&#125;</span><br><span class="line">System.out.println();</span><br><span class="line">methods = c1.getDeclaredMethods();<span class="comment">// 获得本类的所有方法</span></span><br><span class="line"><span class="keyword">for</span> (Method method : methods) &#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;getDeclaredMethods :&quot;</span> + method);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">System.out.println();</span><br><span class="line"><span class="comment">// 获得指定的方法</span></span><br><span class="line"><span class="comment">// 要丢参数进去，方法有重载</span></span><br><span class="line">Method method1 = c1.getMethod(<span class="string">&quot;getName&quot;</span>,<span class="keyword">null</span>);</span><br><span class="line">Method method2 = c1.getMethod(<span class="string">&quot;setName&quot;</span>, String.class);</span><br><span class="line">System.out.println(method1);</span><br><span class="line">System.out.println(method2);</span><br></pre></td></tr></table></figure>

<h3 id="3-4-类的构造器"><a href="#3-4-类的构造器" class="headerlink" title="3.4 类的构造器"></a>3.4 类的构造器</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">Class c1 = Class.forName(<span class="string">&quot;com.mouweng.User&quot;</span>);</span><br><span class="line"><span class="comment">// 获得指定的构造器</span></span><br><span class="line">Constructor[] constructors = c1.getConstructors();</span><br><span class="line"><span class="keyword">for</span> (Constructor constructor : constructors) &#123;</span><br><span class="line">    System.out.println(constructor);</span><br><span class="line">&#125;</span><br><span class="line">constructors = c1.getDeclaredConstructors();</span><br><span class="line"><span class="keyword">for</span> (Constructor constructor : constructors) &#123;</span><br><span class="line">    System.out.println(constructor);</span><br><span class="line">&#125;</span><br><span class="line">System.out.println();</span><br><span class="line"></span><br><span class="line">Constructor constructor1 = c1.getConstructor(<span class="keyword">null</span>);</span><br><span class="line">System.out.println(constructor1);</span><br><span class="line">Constructor constructor2 = c1.getConstructor(String.class,<span class="keyword">int</span>.class,<span class="keyword">int</span>.class);</span><br><span class="line">System.out.println(constructor2);</span><br></pre></td></tr></table></figure>

<h2 id="4-动态创建对象执行方法"><a href="#4-动态创建对象执行方法" class="headerlink" title="4.动态创建对象执行方法"></a>4.动态创建对象执行方法</h2><h3 id="4-1-newInstance方法"><a href="#4-1-newInstance方法" class="headerlink" title="4.1 newInstance方法"></a>4.1 newInstance方法</h3><blockquote>
<p>（1）必须要有默认的无参构造才行</p>
<p>（2）类的构造器能被访问</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Class c1 = Class.forName(<span class="string">&quot;com.mouweng.User&quot;</span>);</span><br><span class="line">User user = (User)c1.newInstance();<span class="comment">// 本质上调用类的无参构造器</span></span><br><span class="line">System.out.println(user);</span><br></pre></td></tr></table></figure>

<h3 id="4-2-通过构造器创建对象"><a href="#4-2-通过构造器创建对象" class="headerlink" title="4.2 通过构造器创建对象"></a>4.2 通过构造器创建对象</h3><blockquote>
<p>获取构造器，通过构造器进行传参创建对象</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过构造器创建对象</span></span><br><span class="line">Constructor constructor = c1.getDeclaredConstructor(String.class, <span class="keyword">int</span>.class, <span class="keyword">int</span>.class);</span><br><span class="line">User user2 = (User) constructor.newInstance(<span class="string">&quot;某翁&quot;</span>, <span class="number">001</span>, <span class="number">18</span>);</span><br><span class="line">System.out.println(user2);</span><br></pre></td></tr></table></figure>

<h3 id="4-3-通过反射调用普通方法"><a href="#4-3-通过反射调用普通方法" class="headerlink" title="4.3 通过反射调用普通方法"></a>4.3 通过反射调用普通方法</h3><blockquote>
<p>通过invoke调用指定的方法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过反射调用普通方法</span></span><br><span class="line">User user3 = (User) c1.newInstance();</span><br><span class="line"><span class="comment">// 通过反射获取方法</span></span><br><span class="line">Method setName = c1.getDeclaredMethod(<span class="string">&quot;setName&quot;</span>,String.class);</span><br><span class="line"><span class="comment">// invoke : 激活的意思,调用了set方法(对象，&quot;方法的值&quot;)</span></span><br><span class="line">setName.invoke(user3,<span class="string">&quot;yifan&quot;</span>);</span><br><span class="line">System.out.println(user3);</span><br></pre></td></tr></table></figure>

<h3 id="4-4-通过反射操作属性"><a href="#4-4-通过反射操作属性" class="headerlink" title="4.4 通过反射操作属性"></a>4.4 通过反射操作属性</h3><blockquote>
<p>setAccessible设置取消安全检测</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过反射操作属性</span></span><br><span class="line">User user4 = (User) c1.newInstance();</span><br><span class="line">Field name = c1.getDeclaredField(<span class="string">&quot;name&quot;</span>);<span class="comment">// 不能直接操作私有属性</span></span><br><span class="line"><span class="comment">// 因为是私有属性，设置取消安全检测</span></span><br><span class="line">name.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">name.set(user4, <span class="string">&quot;fanfan&quot;</span>);</span><br><span class="line">System.out.println(user4);</span><br></pre></td></tr></table></figure>

<h2 id="5-反射操作范型"><a href="#5-反射操作范型" class="headerlink" title="5.反射操作范型"></a>5.反射操作范型</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test07</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test01</span><span class="params">(Map&lt;String, User&gt; map, List&lt;User&gt; list)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;test01&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map&lt;String, User&gt; <span class="title">test02</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;test02&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> NoSuchMethodException </span>&#123;</span><br><span class="line">        <span class="comment">// 获取参数列表的范型</span></span><br><span class="line">        Method method = Test07.class.getMethod(&quot;test01&quot;, Map.class, List.class);</span><br><span class="line">        Type[] genericParameterTypes = method.getGenericParameterTypes();</span><br><span class="line">        <span class="keyword">for</span> (Type genericParameterType : genericParameterTypes) &#123;</span><br><span class="line">            System.out.println(genericParameterType);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 获取返回的范型</span></span><br><span class="line">        method =  Test07.class.getMethod(&quot;test02&quot;,<span class="keyword">null</span>);</span><br><span class="line">        Type genericReturnType = method.getGenericReturnType();</span><br><span class="line">        System.out.println(genericReturnType);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="6-反射获取注解"><a href="#6-反射获取注解" class="headerlink" title="6.反射获取注解"></a>6.反射获取注解</h2><p>可以通过反射来获取注解，对注解进行解析，得到相关信息！</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.mouweng;</span><br><span class="line"><span class="keyword">import</span> java.lang.annotation.*;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test08</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ClassNotFoundException, NoSuchFieldException </span>&#123;</span><br><span class="line">        Class c1 = Class.forName(<span class="string">&quot;com.mouweng.student&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过反射来获得注解</span></span><br><span class="line">        Annotation[] annotations = c1.getAnnotations();</span><br><span class="line">        <span class="keyword">for</span> (Annotation annotation : annotations) &#123;</span><br><span class="line">            <span class="comment">//找到了外面的注解@com.mouweng.TableTemp(value=db_stu)</span></span><br><span class="line">            System.out.println(annotation);</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//获得注解的值 db_stu</span></span><br><span class="line">        TableTemp tableTemp = (TableTemp)c1.getAnnotation(TableTemp.class);</span><br><span class="line">        String value = tableTemp.value();</span><br><span class="line">        System.out.println(value);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获得类的指定的注解</span></span><br><span class="line">        Field field = c1.getDeclaredField(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">        FieldTemp annotation = field.getAnnotation(FieldTemp.class);</span><br><span class="line">        System.out.println(annotation.columnName());</span><br><span class="line">        System.out.println(annotation.type());</span><br><span class="line">        System.out.println(annotation.length());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@TableTemp(&quot;db_stu&quot;)</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">student</span></span>&#123;</span><br><span class="line">    <span class="meta">@FieldTemp(columnName = &quot;db_id&quot;, type = &quot;int&quot;, length = 10)</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line">    <span class="meta">@FieldTemp(columnName = &quot;db_name&quot;, type = &quot;varchar&quot;, length = 20)</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="meta">@FieldTemp(columnName = &quot;db_age&quot;, type = &quot;int&quot;, length = 10)</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">student</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">student</span><span class="params">(<span class="keyword">int</span> id, String name, <span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.id = id;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getId</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(<span class="keyword">int</span> id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.id = id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;id=&quot;</span> + id +</span><br><span class="line">                <span class="string">&quot;, name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 类名的注解</span></span><br><span class="line"><span class="meta">@Target(ElementType.TYPE)</span><span class="comment">//设置作用域</span></span><br><span class="line"><span class="meta">@Retention(RetentionPolicy.RUNTIME)</span><span class="comment">//设置什么级别可以获取</span></span><br><span class="line"><span class="meta">@interface</span> TableTemp&#123;</span><br><span class="line">    <span class="function">String <span class="title">value</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 属性的注解</span></span><br><span class="line"><span class="meta">@Target(ElementType.FIELD)</span></span><br><span class="line"><span class="meta">@Retention(RetentionPolicy.RUNTIME)</span></span><br><span class="line"><span class="meta">@interface</span> FieldTemp&#123;</span><br><span class="line">    <span class="function">String <span class="title">columnName</span><span class="params">()</span></span>;<span class="comment">//列名的注解</span></span><br><span class="line">    <span class="function">String <span class="title">type</span><span class="params">()</span></span>;<span class="comment">//类型</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">length</span><span class="params">()</span></span>;<span class="comment">//长度</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


        </div>

        
            <section class="post-copyright">
                
                
                
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/%E5%8F%8D%E5%B0%84/"># 反射</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2020/08/27/%E5%8C%85%E8%A3%85%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%8C%BA%E5%88%AB/">包装类型和基本类型的区别</a>
            
            
            <a class="next" rel="next" href="/2020/08/27/%E5%BA%8F%E5%88%97%E5%8C%96Serializable/">序列化Serializable</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© Mouweng | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
